<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;
use think\Db;

/**
 * 文章逻辑
 */
class Bussiness extends LogicBase
{
    
    /**
     * 业务范围文章编辑
     */
    public function bussinessEdit($data = [])
    {
        
        $validate_result = $this->validateBussiness->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateBussiness->getError()];
        }
        
        $url = url('category');
        
        $data['content'] = html_entity_decode($data['content']);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        if($handle_text == '新增'){
            
            $data['created_at'] = time();
        }
        
        $data['updated_at'] = time();
        
        $result = $this->modelBussiness->setInfo($data);
        
        $result && action_log($handle_text, '业务范围文章' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelBussiness->getError()];
    }
    

    /**
     * 获取文章信息
     */
    public function getArticleInfo($where = [], $field = true)
    { 
        return $this->modelBussiness->getInfo($where, $field);
    }
    
    
    /**
     * 获取文章列表
     */
    public function getBussinessList($where = [], $field = '*', $order = '')
    {
        $where[DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $category = Db::table('rb_bussiness_category')->where($where)->select();
        
        $category_ids = [];
        
        foreach ($category as $k=>$v){
            $category_ids[] =  $v['id'];
        }
        
        $nwhere[DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $nwhere['category_id'] = array('in',$category_ids);
        
        $list = $this->modelBussiness->getList($nwhere, $field, $order, DB_LIST_ROWS);
        
        return $list;
    }
    
    
    /**
     * 前台获取文章标题列表
     */
    public function getArticleList($where = [], $field = '*', $order = '')
    {
        $where[DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        return Db::table('rb_bussiness')->where($where)->field($field)->order($order)->select();
//        $this->modelBussiness->getList($where, $field, $order, DB_LIST_ROWS);
    }
    
    
    /**
     * 分类编辑
     */
    public function bussinessCategoryEdit($data = [])
    {
        
        $validate_result = $this->validateBussinessCategory->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateBussinessCategory->getError()];
        }
        
        $url = url('category');
        
        $result = $this->modelBussinessCategory->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '文章分类' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelBussinessCategory->getError()];
    }
    
    
    
    /**
     * 获取分类信息
     */
    public function getCategoryInfo($where = [], $field = true)
    { 
        $list =  $this->modelBussinessCategory->getInfo($where, $field);
        
        if($list){
 
            $nwhere['id'] = $list['pid'];
            
            $p_name = $nwhere['id'] == 0 ? '顶级分类':$this->modelBussinessCategory->getInfo($nwhere, 'name')['name'];
        }
        
        $list['p_name']  = $p_name;
        
        return $list;
    }
    
    
    /**
     * 获取分类列表
     */
    public function getBussinessCategory($where = [], $field = '*', $order = '')
    {
        
        $where[DATA_STATUS_NAME] = ['neq', DATA_DELETE];
  
        return $this->modelBussinessCategory->getList($where, $field, $order, DB_LIST_ROWS);
    }
    
    
    /**
     * 获取二级分类列表
     */
    public function getBussinessCategory2($where = [], $field = '*', $order = '')
    {
        $where['pid'] = ['neq',0];
        
        $where[DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $list = $this->modelBussinessCategory->getList($where, $field, $order, DB_LIST_ROWS);
        
        return $list;
    }
    
    
    
    /**
     * 获取文章列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.name|a.describe'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
  
    /**
     * 文章分类删除
     */
    public function articleCategoryDel($where = [])
    {
        
        $result = $this->modelBussinessCategoryCategory->deleteInfo($where);
        
        $result && action_log('删除', '文章分类删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '文章分类删除成功'] : [RESULT_ERROR, $this->modelBussinessCategoryCategory->getError()];
    }
    
    /**
     * 文章删除
     */
    public function articleDel($where = [])
    {
        
        $result = $this->modelBussinessCategory->deleteInfo($where);
        
        $result && action_log('删除', '文章删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '文章删除成功'] : [RESULT_ERROR, $this->modelBussinessCategory->getError()];
    }
    
    /**
     * 获取文章搜索结果
     */
    public function getSearchList($data = [])
    {
        
        $where['name'] = array('like','%'.$data['search'].'%');
        
        //获取新闻列表
        return $this->modelBussiness->getList($where, 'name,id,updated_at,describe', '', 15);
         
    }
    
    
    /**
     * 获取业务文章详情
     * @param type $where
     * @param type $field
     * @return type
     */
    public function getBussinessInfo($where = [], $field = true)
    { 
        return $this->modelBussiness->getInfo($where, $field);
    }
}
